home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cug236.zip / 2UP.C next >
Text File  |  1980-01-02  |  5KB  |  185 lines

  1. /*
  2.     HEADER:        CUG000.00;
  3.     TITLE:        Multi-Column Output Stacker;
  4.     DATE:        05/23/87;
  5.     DESCRIPTION:    "Formats text into one or more columns.  Has several
  6.             command line parameters that can be set.";
  7.     KEYWORDS:    Software tools, Text filters, Text formatters;
  8.     FILENAME:    2UP.C;
  9.     WARNINGS:    "The author claims copyrights and authorizes
  10.             non-commercial use only.";
  11.     AUTHORS:    Eugene H. Mallory, William C. Colley III;
  12.     COMPILERS:    vanilla;
  13. */
  14.  
  15. /*********************************************************************
  16. *                2UP                     *
  17. **********************************************************************
  18. *           COPYRIGHT 1983 EUGENE H. MALLORY             *
  19. *                                     *
  20. * Hacked from BDS C to portable C 23 MAY 1987 by W. C. Colley, III.  *
  21. * This involved removing the -w option as it relies upon a non-         *
  22. * portable direct console I/O call.  In the process, I also reworked *
  23. * the thing into standard C puncutation and optimized various         *
  24. * sections of code so that even a stupid compiler can make a good    *
  25. * executable from it.                             *
  26. *                                     *
  27. *********************************************************************/
  28.  
  29. #include <stdio.h>
  30.  
  31. #define MAXW        132
  32.  
  33. /*  Portability Note:  8-bit systems often don't have header file
  34.     ctype.h.  If your system doesn't have it, uncomment the #define
  35.     NO_CTYPE_H.                                */
  36.  
  37. /*
  38. #define     NO_CTYPE_H
  39. */
  40.  
  41. /*  Portability Note:  Some older compilers don't know the new data
  42.     type void.    If yours is one of these compilers, uncomment the
  43.     following #define:                            */
  44.  
  45. /*
  46. #define void        int
  47. */
  48.  
  49. #ifdef     NO_CTYPE_H
  50. int isdigit();
  51. #else
  52. #include <ctype.h>
  53. #endif
  54.  
  55. #ifndef TRUE
  56. #define TRUE  1
  57. #endif
  58.  
  59. #ifndef FALSE
  60. #define FALSE 0
  61. #endif
  62.  
  63. char string[MAXW + 2];
  64. char array[90][MAXW + 2];
  65. int ncol = 0, col = 0, len = 0, margin = 0, space = 0, width;
  66. int bm = 0, doneflag = FALSE, tm = 0;
  67. void exit();
  68.  
  69. int main(argc,argv)
  70. int argc;
  71. char **argv;
  72. {
  73.     int i, j, k, atoi();
  74.     void error(), strmov();
  75.  
  76. /*********************************************************************
  77. ***          ARGUMENT PROCESSING                   ***
  78. *********************************************************************/
  79.  
  80.     char c, *ss;
  81.     int ii, jj, optionerr;
  82.  
  83.     optionerr = 0;
  84.     for (ii = argc - 1; ii > 0; --ii)
  85.     if (argv[ii][0] == '-') {
  86.         for (ss = &argv[ii][1]; *ss != '\0';) {
  87.         c = *ss++;
  88.         switch (toupper(c)) {
  89.             case 'L':    len = atoi(ss);     break;
  90.  
  91.             case 'C':    col= atoi(ss);    break;
  92.  
  93.             case 'M':    margin = atoi(ss);  break;
  94.  
  95.             case 'T':    tm = atoi(ss);    break;
  96.  
  97.             case 'B':    bm = atoi(ss);    break;
  98.  
  99.             case 'S':    space = atoi(ss);  break;
  100.  
  101.             case 'N':    ncol= atoi(ss);     break;
  102.  
  103.             case 'H':    optionerr = TRUE;  break;
  104.  
  105.             default:    fprintf(stderr,"2UP: Illegal option %c.\n",c);
  106.                 optionerr = TRUE;  break;
  107.         }
  108.         while (isdigit(*ss)) ss++;
  109.         }
  110.         for (jj = ii; jj < (argc-1); ++jj) argv[jj] = argv[jj + 1];
  111.         argc--;
  112.     }
  113.     if (optionerr) {
  114.     fprintf(stderr,"2UP:   Legal options are:\n");
  115.     fprintf(stderr,"-Ln    Page length.\n");
  116.     fprintf(stderr,"-Cn    Column width .\n");
  117.     fprintf(stderr,"-Mn    Margin.\n");
  118.     fprintf(stderr,"-Tn    Top margin.\n");
  119.     fprintf(stderr,"-Bn    Bottom margin.\n");
  120.     fprintf(stderr,"-Sn    Space between columns.\n");
  121.     fprintf(stderr,"-Nn    Number of columns.\n");
  122.     return !0;
  123.     }
  124.     if (ncol == 0) ncol = 2;
  125.     if (len == 0) len = 66;
  126.     if (col == 0) col = (80 - margin - space * (ncol - 1)) / ncol;
  127.     if (len > 88) error("2UP: Too long a page, 88 lines is maximum.");
  128.     if ((margin + ncol * col + (ncol - 1) * space) > MAXW)
  129.     error("2UP: Total width exceeds 132.");
  130.     len = len - tm - bm;
  131.     if (len < 1) error("2UP: Insufficient length.");
  132.  
  133. /*********************************************************************
  134. ***             END OF ARGUMENT PROCESSING               ***
  135. *********************************************************************/
  136. /*********************************/
  137. /*    MAIN LOOP         */
  138. /*********************************/
  139.     width = margin + ncol * col + (ncol - 1) * space;
  140.     do {
  141.     for (i = 1; i <= len; ++i)
  142.         for (j = 1;j <= MAXW + 1; ++j) array[i][j] = ' ';
  143.     for (k = 0; k < ncol; ++k) {
  144.         for (i = 1; i <= len; ++i) {
  145.         if (gets(string))
  146.             strmov(&array[i][margin+k*col+k*space+1],string,col);
  147.         else {
  148.             if (!k && i == 1) return 0;
  149.             doneflag = TRUE;  goto end_of_file;
  150.         }
  151.         }
  152.     }
  153. end_of_file:
  154.     for (i = 1; i <= tm; ++i) puts("\n");
  155.     for (i = 1; i <= len; ++i) {
  156.         for (j = MAXW + 1; j; --j)
  157.         if (array[i][j] != ' ' || j == 1) {
  158.             array[i][j+1] = 0;    break;
  159.         }
  160.         puts(&array[i][1]);
  161.     }
  162.     for (i = bm; i; --i) puts("\n");
  163.     } while (!doneflag);
  164.     return 0;
  165. }
  166.  
  167. void strmov(s1,s2,n)
  168. char *s1,*s2;
  169. int n;
  170. {
  171.     char c;
  172.  
  173.     while (n--) {
  174.     if (!(c = *s2++) || c == '\n') break;
  175.     *s1++ = c;
  176.     }
  177. }
  178.  
  179. void error(msg)
  180. char *msg;
  181. {
  182.     fprintf(stderr,"%s\n",msg);
  183.     exit(!0);
  184. }
  185.